package galaxy.kernel.entity;

import java.util.LinkedHashMap;
import java.util.Map;

import galaxy.kernel.component.ECSComponent;
import galaxy.kernel.component.ECSComponents;

public abstract class AbstractECSEntity implements ECSEntity {

    protected String id;

    private boolean enabled;

    private Map<Class<? extends ECSComponent>, ECSComponent> components = new LinkedHashMap<>();

    public ECSComponents globalcomponents;

    public ECSEntity addComponent(ECSComponent component) {
        if (components.containsKey(component.getClass())) {
            return this;
        }
        if (globalcomponents!=null) {
            globalcomponents.add(component);
        }
        this.components.put(component.getClass(), component);
        return this;
    }

    public void getComponent(){

    }

    public void removeComponent(){}

    public void removeAllComponents(){}

    @Override
    public String getId(){
        return id;
    }

    @Override
    public void setGlobalComponent(ECSComponents components){
        globalcomponents = components;
    }

    @Override
    public ECSEntity setEnabled(boolean b){
        if (enabled == b || globalcomponents == null) {
            return this;
        }

        this.enabled = b;
        if (b) {
            components.values().forEach(c -> {
                globalcomponents.add(c);
            });
        }else {
            components.values().forEach(c -> {
                globalcomponents.delete(c);
            });
        }

        return this;
    }
}
